﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using MichaelKappel.Net.CSharp;
using phudieuhungthinh.BL;
using phudieuhungthinh.BL.Model;

namespace phudieuhungthinh
{
    public partial class SiteMaster : MasterPage
    {
        private const string MenuKey = "MainMenu";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var menuHtml = HelperCache.Get<string>(MenuKey);
                if (menuHtml == null)
                    lock (this)
                    {
                        menuHtml = BuildMenu();
                        HelperCache.Add(MenuKey, menuHtml, HelperCache.CachingTime.ShortTermSliding);
                    }

                pnlMenuContainer.Controls.Add(new LiteralControl(menuHtml));
            }
        }

        private string BuildMenu()
        {
            var menuBuilder = new StringBuilder("<div class='end'>");
            var im = new CategoryImpl();
            List<CategoryMenu> menus = im.GetCategoryMenus();

            menuBuilder.Append("<ul class='sf-menu'>");
            menuBuilder.Append("<li><a href='/'>Trang chủ</a></li>");
            menuBuilder.Append("<li><a href='/gioithieu'>Giới thiệu</a></li>");
            menuBuilder.Append("<li><a>Sản phẩm</a>");
            menuBuilder.Append("<ul>");
            foreach (CategoryMenu item in menus)
            {
                menuBuilder.AppendFormat("<li><a href='/chuyen-muc/{0}/{1}'>{2}</a>", item.Url, string.Empty, item.Name);
                if (item.SubMenus.Count > 0)
                {
                    menuBuilder.Append("<ul>");
                    foreach (CategoryMenu subItem in item.SubMenus)
                    {
                        menuBuilder.AppendFormat("<li><a href='/chuyen-muc/{0}/{1}'>{2}</a></li>", subItem.Url,
                            string.Empty, subItem.Name);
                    }
                    menuBuilder.Append("</ul>");
                }
                menuBuilder.Append("</li>");
            }
            menuBuilder.Append("</ul>");
            menuBuilder.Append("</li>");
            menuBuilder.Append(
                "<li class='longer'><a href='/chuyen-muc/cong-trinh-thuc-hien/'>Công trình thực hiện</a></li>");
            menuBuilder.Append("<li class='longer'><a href='/chuyen-muc/hinh-anh-noi-that/'>Hình ảnh nội thất</a></li>");
            menuBuilder.Append("<li><a href='/tintuc'>Tin tức</a></li>");
            //menuBuilder.Append("<li><a >Tin tức</a></li>");
            menuBuilder.Append("</ul>");
            menuBuilder.Append("</div>");

            return menuBuilder.ToString();
        }
    }
}